package cn.iocoder.yudao.module.measure.dal.dataobject.protocol;

import cn.iocoder.yudao.module.measure.dal.dataobject.upload.BusinessDeviceUploadData;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class DataContentValue {

    private String dataContentType;
    private String dataContentTypeName;
    private double dataContentMinValue;
    private LocalDateTime minValueTime;
    private double dataContentMaxValue;
    private LocalDateTime maxValueTime;
    private double sumValue;
    private double avgValue;

    public DataContentValue() {
        sumValue = 0;
    }

    public DataContentValue(BusinessDeviceUploadData uploadData){
        this.dataContentType = uploadData.getDataType();
        this.dataContentMinValue = Double.valueOf(uploadData.getDataValue());
        this.dataContentMaxValue = Double.valueOf(uploadData.getDataValue());
        this.minValueTime = uploadData.getDataTimestamp();
        this.maxValueTime = uploadData.getDataTimestamp();
        this.sumValue = Double.valueOf(uploadData.getDataValue());
    }

    /**
     * 更新极值记录
     * @param uploadData
     */
    public void updateSpicValue(BusinessDeviceUploadData uploadData) {
//        if (StringUtils.isBlank(dataContentType)) {
//            dataContentType = uploadData.getDataType();
//        }
        String newValueStr = uploadData.getDataValue();
        double newValue = Double.valueOf(newValueStr);
        sumValue += newValue;
        // 因为 大于极大值时不可能小于极小值 反之同理 故这里满足一种情况就不需要再判断另一种 相等时不需要考虑谁大谁小 可忽略
        if(newValue > dataContentMaxValue){
            dataContentMaxValue = newValue;
            maxValueTime = uploadData.getDataTimestamp();
        } else if(newValue < dataContentMinValue){
            dataContentMinValue = newValue;
            minValueTime = uploadData.getDataTimestamp();
        }
    }
}
